package com.sywb.chuangyebao.contract;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Topic;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AskActivity;

import org.bining.footstone.log.Logger;
import org.bining.footstone.utils.ToastUtils;

public interface TopicContract {

    interface TopicView extends BaseQuestionContract.BaseQuestionView {

        int getTopicId();

        void setFollowView(boolean isConcern);
    }

    class Presenter extends BaseQuestionContract.Presenter<TopicView> {

        private ImageView ivTopic;
        private TextView tvTopic;
        private TextView tvTopicInfo;

        private Topic topic;

        @Override
        public void onStart() {
            super.onStart();
            category = 0;
            topic_id = mView.getTopicId();

            View headView = mActivity.getLayoutInflater().inflate(R.layout.layout_top_head, null);
            headView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ivTopic = headView.findViewById(R.id.iv_topic);
            tvTopic = headView.findViewById(R.id.tv_topic);
            tvTopicInfo = headView.findViewById(R.id.tv_topic_info);
            recyclerAdapter.setHeaderView(headView);

            initData();
        }

        @Override
        public void initData() {
            super.initData();
            pageNo = 1;
            getTopic(topic_id);
            listQuestions();
        }

        @Override
        public void onRecyclerItemClick(int i) {
            if (i > 0) {
                super.onRecyclerItemClick(i - 1);
            }
        }

        @Override
        public void onRecyclerItemChildClick(View view, int postion) {
            if (postion > 0) {
                super.onRecyclerItemChildClick(view, postion - 1);
            }
        }

        public void getTopic(int topicId) {
            HttpUtils.getTopic(topicId, new HttpCallback<Topic>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onSuccess(Topic model) {
                    if (model == null) {
                        return;
                    }
                    topic = model;
                    GlideUtils.getNormalIamgeToUrl(mActivity, model.logo, ivTopic);
                    tvTopic.setText("#" + model.name + "#");
                    tvTopicInfo.setText("讨论：" + model.question_num + "   |   粉丝" + model.follow_num);
                    mView.setFollowView(model.isConcern);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

            });
        }

        public void goAsk() {
            if (topic != null && Utils.checkIsNext(mView.getMyFragmentManager())) {
                mView.advance(AskActivity.class, AskActivity.COMING_TYPE_ASK, "", "", 0, topic.name, topic_id);
            }
        }

        public void follow() {
            //判断是否登录
            if (!Utils.checkIsLogin()) {
                return;
            }

            HttpUtils.follow(topic_id, Constants.TOPIC_TYPE_TOPIC, !topic.isConcern, new HttpCallback<Object>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onSuccess(Object s) {
                    if (mView != null) {
                        ToastUtils.show(mContext, topic.isConcern ? "取消关注" : "关注成功");
                        topic.isConcern = !topic.isConcern;
                        mView.setFollowView(topic.isConcern);
                        getTopic(topic_id);
                    }
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }

        public void goConsult() {
            if(topic==null){
                return ;
            }
            Utils.goConsult(this, mView, topic.project_id, 0, Constants.COOKIE_TOPIC);
        }
    }
}

